<div class="admin-main layui-anim layui-anim-upbit">
    <fieldset class="layui-elem-field layui-field-title">
        <legend>{:lang('admin')}{:lang('list')}</legend>
    </fieldset>
    <table class="layui-table" id="list" lay-filter="list"></table>
</div>

<script type="text/html" id="barDemo">
    <a href="{:url('adminEdit')}?id={{d.id}}" class="layui-btn layui-btn-xs">{:lang('edit')}</a>
    {{# if(d.id==1){ }}
    <a href="#" class="layui-btn layui-btn-xs layui-btn-disabled">{:lang('del')}</a>
    {{# }else{  }}
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">{:lang('del')}</a>
    {{# } }}
</script>
<script type="text/html" id="status">
    {{# if(d.admin_id==1){ }}
    <input type="checkbox" disabled name="status" value="{{d.id}}" lay-skin="switch" lay-text="开启|关闭"
           lay-filter="status" checked>
    {{# }else{  }}
    <input type="checkbox" name="status" value="{{d.id}}" lay-skin="switch" lay-text="开启|关闭" lay-filter="status" {{
           d.status== 1 ? 'checked' : '' }}>
    {{# } }}
</script>

<script type="text/html" id="topBtn">
    <a href="{:url('adminEdit')}?id=0" class="layui-btn layui-btn-sm">{:lang('add')}{:lang('admin')}</a>
</script>
<script>
    layui.use(['table', 'form'], function () {
        var table = layui.table, form = layui.form, $ = layui.jquery;
        //表单
        var tableIn = table.render({
            elem: '#list',
            url: '{:url("adminList")}',
            method: 'post',
            toolbar: '#topBtn',
            title: '{:lang("admin")}{:lang("list")}',
            cols: [[
                {field: 'id', title: 'ID', width: 60, fixed: true}
                , {field: 'username', title: '用户名', width: 80}
                , {field: 'title', title: '{:lang("userGroup")}', width: 200}
                , {field: 'last_time', title: '最后登录时间', width: 200}
                , {field: 'last_ip', title: '{:lang("ip")}', width: 200}
                , {field: 'status', title: '{:lang("status")}', width: 150, toolbar: '#status'}
                , {width: 160, align: 'center', toolbar: '#barDemo'}
            ]]
        });

        //状态
        form.on('switch(status)', function (obj) {
            loading = layer.load(1, {shade: [0.1, '#fff']});
            var id = this.value;
            var status = obj.elem.checked === true ? 1 : 0;
            $.post('{:url("adminState")}', {'id': id, 'status': status}, function (res) {
                layer.close(loading);
                if (res.code == 1) {
                    tableIn.reload();
                } else {
                    layer.msg(res.msg, {time: 1000, icon: 2});
                    return false;
                }
            })
        });

        //删除
        table.on('tool(list)', function (obj) {
            var data = obj.data;
            if (obj.event === 'del') {
                layer.confirm('{:lang("Are you sure you want to delete it")}', function (index) {
                    $.post("{:url('adminDel')}", {admin_id: data.admin_id}, function (res) {
                        if (res.code == 1) {
                            layer.msg(res.msg, {time: 1000, icon: 1});
                            obj.del();
                        } else {
                            layer.msg(res.msg, {time: 1000, icon: 2});
                        }
                    });
                    layer.close(index);
                });
            }
        });

    });
</script>
</body>
</html>